
var xhr;
function checkCode(){

    var password=document.getElementById('password').value;
    if(password!=''){

        xhr=new XMLHttpRequest();
        xhr.onreadystatechange=process1;
        xhr.open("GET","codeIsProper?password="+password,true);
        xhr.send(null);
    }
    else{
        var msg=document.getElementById("isProperCode");
        msg.innerText='';
        msg=document.getElementById("isRepeatedCode");
        msg.innerText='';
    }
}
function process1(){
    if(xhr.readyState ==4){
        if(xhr.status==200){
            var responseInfo=xhr.responseText;

            var msg=document.getElementById("isProperCode");
            if(responseInfo=="Yes"){
                msg.classList.add("yesmsg");
                msg.innerText='密码可用';
                msg.style.color='green';
            }
            else {
                msg.classList.add('nomsg');
                msg.innerText=responseInfo;
                msg.style.color='red';
            }
        }
    }
}

var xhr;
function checkRepeatedCode() {
    var repeatedPassword = document.getElementById('repeatedPassword').value;
    var password = document.getElementById('password').value;
    if (repeatedPassword != '' && password != '') {
        xhr = new XMLHttpRequest();
        xhr.onreadystatechange = process2;
        xhr.open("GET", "repeatedCodeIsProper?repeatedPassword=" + repeatedPassword + "&password=" + password, true);
        xhr.send(null);
    }
    else{
        var msg=document.getElementById("isRepeatedCode");
        msg.innerText='';
    }
}
function process2(){
    if(xhr.readyState ==4){
        if(xhr.status==200){

            var responseInfo=xhr.responseText;
            var msg=document.getElementById("isRepeatedCode");
            if(responseInfo=='Yes'){
                msg.classList.add("yesmsg");
                msg.innerText='密码输入正确';
                msg.style.color='green';
            }
            else {
                msg.classList.add('nomsg');
                msg.innerText=responseInfo;
                msg.style.color='red';
            }
        }
    }
}

var xhr;
function checkAccount(){

    var username=document.getElementById('username').value;
    if(username!=''){

        xhr=new XMLHttpRequest();
        xhr.onreadystatechange=process3;
        xhr.open("GET","accountIsExist?username="+username,true);
        xhr.send(null);
    }
    else{
        var msg=document.getElementById("isAccountExist");
        msg.innerText='';
    }
}
function process3(){
    if(xhr.readyState ==4){
        if(xhr.status==200){
            var responseInfo=xhr.responseText;
            var msg=document.getElementById("isAccountExist");
            if(responseInfo=="Yes"){
                msg.classList.add('yesmsg');
                msg.innerText='用户名可用';
                msg.style.color='green';
            }
            else {
                msg.classList.add('nomsg');
                msg.innerText='用户名已被注册';
                msg.style.color='red';
            }
        }
    }
}
var xhr;
function checkPhone(){

    var phone=document.getElementById('phone').value;
    if(phone!=''){
        xhr=new XMLHttpRequest();
        xhr.onreadystatechange=process4;
        xhr.open("GET","phoneIsProper?phone="+phone,true);
        xhr.send(null);
    }
    else{
        var msg=document.getElementById("isProperPhone");
        msg.innerText='';
    }
}
function process4(){
    if(xhr.readyState ==4){
        if(xhr.status==200){
            var responseInfo=xhr.responseText;
            var msg=document.getElementById("isProperPhone");
            if(responseInfo=="Yes"){
                msg.innerText='';
            }
            else {
                msg.classList.add('nomsg');
                msg.innerText='手机号码格式不正确！';
                msg.style.color='red';
            }
        }
    }
}

var xhr;
function checkEmail(){
    var email=document.getElementById('email').value;
    if(email!=''){
        xhr=new XMLHttpRequest();
        xhr.onreadystatechange=process5;
        xhr.open("GET","emailIsProper?email="+email,true);
        xhr.send(null);
    }
    else{
        var msg=document.getElementById("isProperEmail");
        msg.innerText='';
    }
}
function process5(){
    if(xhr.readyState ==4){
        if(xhr.status==200){

            var responseInfo=xhr.responseText;
            var msg=document.getElementById("isProperEmail");
            if(responseInfo=="Yes"){
                msg.innerText='';
            }
            else {
                msg.classList.add('nomsg');
                msg.innerText='邮箱格式不正确！';
                msg.style.color='red';
            }
        }
    }
}


var xhr;
var boolCoupons = new Array(5).fill(false);
var flag;
function showCoupon(radio){
    var id=radio.value;
    console.log("id:"+id);
    flag=1;
    if(id!=''){
        if(boolCoupons[id]){
            flag=0;
            boolCoupons[id]=0;
        }
        else{
            flag=1;
            boolCoupons[id]=1;
        }
        xhr=new XMLHttpRequest();
        xhr.onreadystatechange=process6;
        console.log(flag);
        xhr.open("GET","useCoupon?id="+id+"&flag="+flag,true);
        xhr.send(null);
    }
    else{

    }
}
function process6(){
    if(xhr.readyState ==4){
        if(xhr.status==200){
            if(flag==0){
                const radios = document.querySelectorAll('input[type="radio"]');
                radios.forEach((radio) => {
                    radio.checked=false;
                });
                var notice=document.getElementById("couponNotice")
                notice.innerText="";

            }

            var responseInfo=xhr.responseText;
            console.log(responseInfo);
            var msg=document.getElementById("subtotal");
            var notice=document.getElementById("couponNotice")
            msg.classList.add('nomsg');
            msg.innerText=responseInfo;
            var num=-1;
            for (let i = 0; i < boolCoupons.length; i++) {
                if(boolCoupons[i]){
                    num=i;
                    break;
                }
            }
            if(num!=-1)
                notice.innerText="选择了优惠券"+num;
        }
    }

}